

"""
traverse resource hierarchy
"""

import types
from whiff import gateway
from whiff import whiffenv
from whiff.middleware import misc

def listResources(env, start_response):
    start_response("200 OK", [('Content-Type', 'text/html')])
    try:
        roleNames = gateway.getResource(env, ["grades"])
    except gateway.AccessDenied:
        yield "you are not logged in as a valid user -- no access to any grade resource.<br>\n"
    else:
        yield "<table border='1'>\n"
        yield "<tr><th colspan='3'>%s</th></tr>\n" % (roleNames,)
        # student resources
        courses = gateway.getResource(env, ["grades", "student"])
        attends = courses
        if not attends:
            attends = "doesn't attend any courses"
        yield "<tr><th align='right'>attends<br> courses</th> <th colspan='2'>%s</th></tr>\n" % (attends,)
        for course in courses:
            yield resourceForm(env, ["grades", "student", course])
        # instructor resources
        courses = gateway.getResource(env, ["grades", "instructor"])
        teaches = courses
        if not teaches:
            teaches = "doesn't teach any courses"
        yield "<tr><th align='right'>teaches<br> courses:</th> <th colspan='2'>%s</th></tr>\n" % teaches
        for course in courses:
            students = gateway.getResource(env, ["grades", "instructor", course])
            yield "<tr><td align='right'>course %s</td> <td colspan='2'> students: %s</td></tr>\n" % (
                course, students)
            for student in students:
                yield resourceForm(env, ["grades", "instructor", course, student])
        # advisor resources
        students = gateway.getResource(env, ["grades", "advisor"])
        advises = students
        if not advises:
            advises = "doesn't advise any students"
        yield "<tr><th align='right'>advises<br>students</th> <th colspan='2'> %s </th></tr>\n" % (advises,)
        for student in students:
            courses = gateway.getResource(env, ["grades", "advisor", student])
            yield "<tr><td align='right'>student %s</td> <td colspan='2'>courses:  %s</td></tr>\n" % (
                student, courses)
            for course in courses:
                yield resourceForm(env,["grades", "advisor", student, course])
        yield "</table>\n"


def resourceForm(env, path):
    userid = whiffenv.cgiGet(env, "userid")
    password = whiffenv.cgiGet(env, "password")
    stringPath = "/".join(path)
    currentValue = gateway.getResource(env, path)
    D = {
        "userid": userid,
        "password": password,
        "stringPath": stringPath, 
        "currentValue": currentValue, 
        "path": path
        }
    return FORMTEMPLATE % D

FORMTEMPLATE = """
<tr>
<td></td>
<td align="right" width="400"> <code>%(path)s:</code> </td>
<td>
<form action="updateResource">
<input type="hidden" name="userid" value="%(userid)s">
<input type="hidden" name="password" value="%(password)s">
<input type="hidden" name="stringPath" value="%(stringPath)s">
<input name="currentValue" value="%(currentValue)s">
<input type="submit" name="change" value="change">
</form>
</td>
</tr>
"""

__wsgi__ = listResources
